import pymysql

# 查询数据
# fetchone(): 该方法获取下一个查询结果集。结果集是一个对象
# fetchall(): 接收全部的返回结果行.
# rowcount: 这是一个只读属性，并返回执行execute()方法后影响的行数。

try:
    con = pymysql.connect(host="localhost", user="root", password="rootroot", db="blog")
    print(con)
    cursor = con.cursor()

    # sql = 'select * from student where age>=23'
    sql = 'select * from student where age>=13'

    # 执行sql
    cursor.execute(sql)
    # 获取查询结果
    results = cursor.fetchall()
    for row in results:
        sno = row[0]
        sname = row[1]
        sex = row[2]
        age = row[3]
        score = row[4]
        # 输出
        print('sno:', sno, 'sname:', sname, 'sex:', sex, 'age:', age, 'score:', score)
    print("查询成功")
except Exception as e:
    print(e)
    con.rollback()
    print("查询失败")
finally:
    # 关闭数据库连接
    con.close()
